﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using ly.BLL;

namespace Xiangmu
{
    public partial class Login : System.Web.UI.Page
    {
        ly.BLL.UserInfo user = new UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                Session["username"] = null;
            }
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            string code = txtCode.Text.Trim().ToUpper();
            string rightCode = Session["Code"].ToString();
            if (code != rightCode)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('验证码输入错误！');</script>");
                return;
            }
            String username = txtName.Value.Trim();
            String pwd = txtPsw.Value.Trim();
            string type = ddlrole.Text.Trim();
            if (type == "普通用户")
            {
                bool flag = user.Exists(username, pwd, type);
                if (flag)
                {
                    Session["username"] = txtName.Value.Trim();
                    Session["type"] = ddlrole.Text.Trim();
                    Response.Write("<script>alert('登录成功');</script>");
                    Response.Redirect("../QianTai/Index.aspx");
                }
            }

            else if (type == "超级管理员")
            {
             bool flag = user.Exists(username, pwd, type);
                if (flag)
                {
                    Session["username"] = txtName.Value.Trim();
                    Session["type"] = ddlrole.Text.Trim();
                    Response.Write("<script>alert('登录成功');</script>");
                    Response.Redirect("index.aspx");
                }
            }
            //String sql = "select * from Students where Sname=@username and Spwd=@pwd";
            //SqlParameter[] paras = new SqlParameter[]{
            //    new SqlParameter("@username",username),
            //    new SqlParameter("@pwd",pwd)
            //};
            //int cou = Convert.ToInt32(DBHelper.GetScaler(sql, paras));

            else
            {
                Response.Write("<script>alert('您输入的密码错误，请核对后重新登陆');</script>");
            }

        }
    }
}